package com.dmy.chat.server.netty.coder;

import com.alibaba.fastjson.JSON;
import com.dmy.chat.server.netty.message.Messages;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;

import java.util.List;

/**
 * create by YCQ for 2020/1/13 9:15
 */
public class WebsocketDecoder extends MessageToMessageDecoder<WebSocketFrame> {
    @Override
    protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
        try {
            if(msg instanceof TextWebSocketFrame){
                String text = ((TextWebSocketFrame) msg).text();
                Messages messages = JSON.parseObject(text, Messages.class);
                out.add(messages);
            }
        }catch (Exception ex){
            ctx.pipeline().remove(this);
        }

    }
}
